package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Info;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("ShoppingCart")
@Api(tags = "c端购物车相关接口")
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 根据用户id查询购物车
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车")
    public  Result<List<ShoppingCart>> list() {

        return Result.success(shoppingCartService.listShoppingCart());
    }


    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
//    @CacheEvict(cacheNames = "dishCache", allEntries = true)
//    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车：{}", shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);//后绪步骤实现
        return Result.success();
    }



    @PostMapping("/sub")
    @ApiOperation("删除指定商品")
//    @CacheEvict(cacheNames = "dishCache", allEntries = true)
//    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public  Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除指定商品：{}", shoppingCartDTO);
        shoppingCartService.deleteShoppingCart(shoppingCartDTO);//后绪步骤实现
        return Result.success();
    }

    @DeleteMapping("/clean")
    public Result clean(){
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }
}
